package cn.bopomo.dealer.base.db.impl;

import java.util.ArrayList;
import java.util.List;

import cn.bopomo.dealer.base.db.BaseDAO;
import cn.bopomo.dealer.base.db.IOperatorDAO;
import cn.bopomo.dealer.base.exception.ExceptionCode;
import cn.bopomo.dealer.base.exception.SystemException;
import cn.bopomo.dealer.base.util.Page;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.OperatorDetail;

public class OperatorDAOImpl extends BaseDAO implements IOperatorDAO {

	public String insertOperator(OperatorDetail od) {
		try {
			this.insertOrUpdate(od);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public String updateOperator(OperatorDetail od) {
		try {
			this.insertOrUpdate(od);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public String deleteOperator(OperatorDetail od) {
		try {
			this.getHibernateTemplate().delete(od);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	/**
	 * 根据传入的操作员信息查找它的子操作员
	 * @param od
	 * @return
	 * @throws SystemException
	 */
	public List selectOperatorsByParent(OperatorDetail od) throws SystemException {
		String hql = "from OperatorDetail where operatorDetail = ? and isDelete != '2'";
		return this.selectAll(hql,new Object[]{od});
	}
	
	
	/**
	 * 分页查找当前操作员子类列表
	 * @param od
	 * @param pageSize
	 * @param rowsPerPage
	 * @return
	 * @throws SystemException
	 */
	public Page selectOperatorListByParent(OperatorDetail od, int pageSize, int rowsPerPage) throws SystemException {
		String hql = "from OperatorDetail where operatorDetail = ? and isDelete != '2'";
		List paraList = new ArrayList();
		paraList.add(od);
		try {
			return this.selectSectionData(hql,null,paraList,null,"id",true,pageSize,rowsPerPage);
		} catch (Exception e) {
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
	}
	
	public List selectOperatorsByAuthorize(String tag) throws SystemException {
		String hql = "from OperatorDetail where authorize like '%" + tag + "/%'";
		
		return this.selectAll(hql, null);
	}
	
	public List selectOperatorsByModule(String tag) throws SystemException {
		String hql = "from OperatorDetail where moduleAuthorize like '%" + tag + "%'";
		
		return this.selectAll(hql, null);
	}

	public OperatorDetail selectOperator(String loginName, String pwd) {
		String hql = "from OperatorDetail ";
		Object[] condition = new Object[]{"loginName","password","isDelete"};
		List param = new ArrayList();
		param.add(loginName);
		param.add(pwd);
		param.add("2");
		Object[] logic = new Object[]{"=", "=", "!="};
		
		String whereSQL = this.generateWhereSQL(condition,param,logic,null,false);
		Object operator = null;
		try {
			operator = this.sectionFirstObject(hql,whereSQL,param);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (operator != null){
			return (OperatorDetail)operator;
		}
		return null;
		
	}

	public String selectOperator(String loginName) {
		String hql = "from OperatorDetail ";
		Object[] condition = new Object[]{"loginName"};
		List param = new ArrayList();
		param.add(loginName);
		Object[] logic = new Object[]{"="};
		
		String whereSQL = this.generateWhereSQL(condition,param,logic,null,false);
		Object operator = null;
		try {
			operator = this.sectionFirstObject(hql,whereSQL,param);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResultCode.OP_SYSTEM_DB_ERR;
		}
		if (operator == null){
			return ResultCode.OP_SUCCESS;
		}
		return ResultCode.OP_FAILED;
	}

	public OperatorDetail selectOperator(Integer uid) {
		String hql = "from OperatorDetail ";
		Object[] condition = new Object[]{"id"};
		List param = new ArrayList();
		param.add(uid);
		Object[] logic = new Object[]{"="};
		
		String whereSQL = this.generateWhereSQL(condition,param,logic,null,false);
		Object operator = null;
		try {
			operator = this.sectionFirstObject(hql,whereSQL,param);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		if (operator != null){
			return (OperatorDetail)operator;
		}
		return null;
	}

}
